﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class GPSModels : ClassManager.SessionManagerPage
    {
        private void GetAllModels()
        {
            GPSModelGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.GpsModelsProvider.GetAll();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetAllModels();

                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.GpsStockColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.GPSModelGrid.MasterTableView.SortExpressions.AddSortExpression(expression);               
            }
        }

        protected void GPSModelGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetAllModels();
            GPSModelGrid.Height = Unit.Parse(HiddenFieldHeight.Value.ToString());
        }

        protected void GPSModelGrid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.GpsModels GpsModelEntity = ClassManager.InvokeEntityManager.Invoke_GpsModel_Entity(e);
                GpsModelEntity.GpsModelId = GPSTrackingDBV2Custom.Data.DataRepository.GpsModelsProvider.GetNextID();
                GPSTrackingDBV2.Data.DataRepository.GpsModelsProvider.Insert(GpsModelEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede insertar la información."); 
            }                           
        }

        protected void GPSModelGrid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.GpsModels GpsModelEntity = ClassManager.InvokeEntityManager.Invoke_GpsModel_Entity(e);                

                GPSTrackingDBV2.Data.DataRepository.GpsModelsProvider.Update(GpsModelEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede actualizar la información.");
            }
        }

        protected void GPSModelGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.GpsModels GpsModelEntity = ClassManager.InvokeEntityManager.Invoke_GpsModel_Entity(e);
                
                GPSTrackingDBV2.Data.DataRepository.GpsModelsProvider.Delete(GpsModelEntity.GpsModelId);            
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede eliminar el registro porque el modelo porque se encuentra asociados a otros registros.");
            }
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            GPSModelGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in GPSModelGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }


            GPSModelGrid.MasterTableView.Rebind();
        }


        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_miniError", "objWindowsMessageManager.show_miniError('" + msg + "','Modelos ...');", true);
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }
        
    }
}